package br.com.webcrm.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import br.com.webcrm.dao.QuestAlternativaDAO;
import br.com.webcrm.model.QuestAlternativa;

@Repository(value = "questAlternativaDAO")
public class QuestAlternativaDAOImpl extends BaseDAOImpl<QuestAlternativa, Integer> implements QuestAlternativaDAO {
	
	@Autowired
	public QuestAlternativaDAOImpl(
			@Qualifier("sessionFactory") SessionFactory factory) {
		super.setSessionFactory(factory);
	}

	@Override
	public List<QuestAlternativa> consultarPorIdPergunta(Integer idPergunta){
		String[] nomes = {"idPergunta"};
		Object[] valores = {idPergunta};
		return this.consultarPorNamedQueryEParametros("QuestAlternativa.findByIdPergunta", nomes, valores);
	}

	@Override
	public Integer consultarSeqUltimaAlternativaPorIdPergunta(Integer idPergunta) {
		Criteria crit = getSession().createCriteria(QuestAlternativa.class);
		crit.add(Restrictions.eq("pergunta.idPergunta", idPergunta));
		crit.setProjection(Projections.max("sequenciaAlternativa"));
		return (Integer) crit.uniqueResult();
	}

	
}
